**************************************************************
***** Name:        do_merge_team2.do      		     	 *****
***** Description: This do file creates data at the	 	 *****
***** team level (part 2)								 *****
**************************************************************

clear all
global path "C:\Users\patry\OneDrive\Desktop\Mario replication files"
cd "$path"
set more off


* 1.0 Call team data

use "Data/temp data/team_temp.dta", clear

* 2.0 Merge skills data 

merge 1:1 team_id pair round using "Data/temp data/team_skills.dta", nogen

* 3.0 Merge coordination data

merge 1:1 team_id pair round using "Data/raw data/team_coordination.dta", nogen

* 3.0 Other cleaning

* 3.1 Skills label

gen SKILLS = . 
label var SKILLS "************************************"
order SKILLS, after(round_diff)

* 3.2 Experimental sample indicator

gen sample = pair!=3
la var sample "Experimental sample"
  
* 3.3 Phase 2 score control

drop phase2score
levelsof team_id , local(teams)

gen phase2score = .

foreach t in `teams' {
	
	* 1-2 control:
	qui sum totalingred if phase2==1 & team_id=="`t'" & (inlist(player_1,1,2) | inlist(player_2,1,2))
	qui replace phase2score = r(mean) if  phase3==1 & team_id=="`t'" & player_1==1 & player_2==2
	
	* 3-4 control:
	qui sum totalingred if phase2==1 & team_id=="`t'" & (inlist(player_1,3,4) | inlist(player_2,3,4))
	qui replace phase2score = r(mean) if  phase3==1 & team_id=="`t'" & player_1==3 & player_2==4
	
	* 1-3 control:
	qui sum totalingred if phase2==1 & team_id=="`t'" & (inlist(player_1,1,3) | inlist(player_2,1,3))
	qui replace phase2score = r(mean) if  phase3==1 & team_id=="`t'" & player_1==1 & player_2==3
	
	* 1-4 control:
	qui sum totalingred if phase2==1 & team_id=="`t'" & (inlist(player_1,1,4) | inlist(player_2,1,4))
	qui replace phase2score = r(mean) if  phase3==1 & team_id=="`t'" & player_1==1 & player_2==4
	
	* 2-4 control:
	qui sum totalingred if phase2==1 & team_id=="`t'" & (inlist(player_1,2,4) | inlist(player_2,2,4))
	qui replace phase2score = r(mean) if  phase3==1 & team_id=="`t'" & player_1==2 & player_2==4
	
	* 2-3 control:
	qui sum totalingred if phase2==1 & team_id=="`t'" & (inlist(player_1,2,3) | inlist(player_2,2,3))
	qui replace phase2score = r(mean) if  phase3==1 & team_id=="`t'" & player_1==2 & player_2==3
	
	* 2-0 control:
	qui sum totalingred if phase2==1 & team_id=="`t'" & (inlist(player_1,2,0) | inlist(player_2,2,0))
	qui replace phase2score = r(mean) if  phase3==1 & team_id=="`t'" & player_1==2 & player_2==0
	
	* 3-0 control:
	qui sum totalingred if phase2==1 & team_id=="`t'" & (inlist(player_1,3,0) | inlist(player_2,3,0))
	qui replace phase2score = r(mean) if  phase3==1 & team_id=="`t'" & player_1==3 & player_2==0
	
	* 4-0 control:
	qui sum totalingred if phase2==1 & team_id=="`t'" & (inlist(player_1,4,0) | inlist(player_2,4,0))
	qui replace phase2score = r(mean) if  phase3==1 & team_id=="`t'" & player_1==4 & player_2==0
	
	* 6-2 control:
	qui sum totalingred if phase2==1 & team_id=="`t'" & (inlist(player_1,6,2) | inlist(player_2,6,2))
	qui replace phase2score = r(mean) if  phase3==1 & team_id=="`t'" & player_1==6 & player_2==2
	
	* 6-3 control:
	qui sum totalingred if phase2==1 & team_id=="`t'" & (inlist(player_1,6,3) | inlist(player_2,6,3))
	qui replace phase2score = r(mean) if  phase3==1 & team_id=="`t'" & player_1==6 & player_2==3
	
	* 6-4 control:
	qui sum totalingred if phase2==1 & team_id=="`t'" & (inlist(player_1,6,4) | inlist(player_2,6,4))
	qui replace phase2score = r(mean) if  phase3==1 & team_id=="`t'" & player_1==6 & player_2==4
	
	* 0-2 control:
	qui sum totalingred if phase2==1 & team_id=="`t'" & (inlist(player_1,0,2) | inlist(player_2,0,2))
	qui replace phase2score = r(mean) if  phase3==1 & team_id=="`t'" & player_1==0 & player_2==2

}

la var phase2score "Avg performance in phase 2"


* 3.4 Phase 2 coordination control

levelsof team_id , local(teams)

foreach x in cfail  { 

	gen phase2`x' = .

	foreach t in `teams' {
		
		* 1-2 control:
		qui sum `x' if phase2==1 & team_id=="`t'" & (inlist(player_1,1,2) | inlist(player_2,1,2))
		qui replace phase2`x' = r(mean) if  phase3==1 & team_id=="`t'" & player_1==1 & player_2==2
		
		* 3-4 control:
		qui sum `x' if phase2==1 & team_id=="`t'" & (inlist(player_1,3,4) | inlist(player_2,3,4))
		qui replace phase2`x' = r(mean) if  phase3==1 & team_id=="`t'" & player_1==3 & player_2==4
		
		* 1-3 control:
		qui sum `x' if phase2==1 & team_id=="`t'" & (inlist(player_1,1,3) | inlist(player_2,1,3))
		qui replace phase2`x' = r(mean) if  phase3==1 & team_id=="`t'" & player_1==1 & player_2==3
		
		* 1-4 control:
		qui sum `x' if phase2==1 & team_id=="`t'" & (inlist(player_1,1,4) | inlist(player_2,1,4))
		qui replace phase2`x' = r(mean) if  phase3==1 & team_id=="`t'" & player_1==1 & player_2==4
		
		* 2-4 control:
		qui sum `x' if phase2==1 & team_id=="`t'" & (inlist(player_1,2,4) | inlist(player_2,2,4))
		qui replace phase2`x' = r(mean) if  phase3==1 & team_id=="`t'" & player_1==2 & player_2==4
		
		* 2-3 control:
		qui sum `x' if phase2==1 & team_id=="`t'" & (inlist(player_1,2,3) | inlist(player_2,2,3))
		qui replace phase2`x' = r(mean) if  phase3==1 & team_id=="`t'" & player_1==2 & player_2==3
		
		* 2-0 control:
		qui sum `x' if phase2==1 & team_id=="`t'" & (inlist(player_1,2,0) | inlist(player_2,2,0))
		qui replace phase2`x' = r(mean) if  phase3==1 & team_id=="`t'" & player_1==2 & player_2==0
		
		* 3-0 control:
		qui sum `x' if phase2==1 & team_id=="`t'" & (inlist(player_1,3,0) | inlist(player_2,3,0))
		qui replace phase2`x' = r(mean) if  phase3==1 & team_id=="`t'" & player_1==3 & player_2==0
		
		* 4-0 control:
		qui sum `x' if phase2==1 & team_id=="`t'" & (inlist(player_1,4,0) | inlist(player_2,4,0))
		qui replace phase2`x' = r(mean) if  phase3==1 & team_id=="`t'" & player_1==4 & player_2==0
		
		* 6-2 control:
		qui sum `x' if phase2==1 & team_id=="`t'" & (inlist(player_1,6,2) | inlist(player_2,6,2))
		qui replace phase2`x' = r(mean) if  phase3==1 & team_id=="`t'" & player_1==6 & player_2==2
		
		* 6-3 control:
		qui sum `x' if phase2==1 & team_id=="`t'" & (inlist(player_1,6,3) | inlist(player_2,6,3))
		qui replace phase2`x' = r(mean) if  phase3==1 & team_id=="`t'" & player_1==6 & player_2==3
		
		* 6-4 control:
		qui sum `x' if phase2==1 & team_id=="`t'" & (inlist(player_1,6,4) | inlist(player_2,6,4))
		qui replace phase2`x' = r(mean) if  phase3==1 & team_id=="`t'" & player_1==6 & player_2==4
		
		* 0-2 control:
		qui sum `x' if phase2==1 & team_id=="`t'" & (inlist(player_1,0,2) | inlist(player_2,0,2))
		qui replace phase2`x' = r(mean) if  phase3==1 & team_id=="`t'" & player_1==0 & player_2==2

	}

	la var phase2`x' "Avg `x' in phase 2"
}




* 4.0 Create heterogeneity indicators

* Round difficulty
drop round_diff_low round_diff_high
qui sum round_diff, d
gen round_diff_low =    round_diff <= r(p50)
gen round_diff_high =   round_diff > r(p50)
	 

* Create round indicators
forv r = 1/12 {
	gen round_`r' = round==`r'
}
foreach x in ai newhire ai_direct ai_spillover newhire_direct newhire_spillover control tacit explicit {
	forv r = 1/12 {
		gen `x'Xround_`r' = `x' * round_`r'
	}
}





 * Skills identifiers
 gen z_game_p234= (z_game_p2 + z_game_p3 + z_game_p4)/3
  la var z_game_p234 "Average skill, remaining players"


  * Player replaced

  gen z_game_p1_low = z_game_p1<-.2
  gen z_game_p1_middle = inrange(z_game_p1, -.2 , .38)
  gen z_game_p1_high = z_game_p1>.38 & !missing(z_game_p1)
  
  * Player remaining
  
  gen z_game_p2_low = z_game_p2<.035
  gen z_game_p2_middle = inrange(z_game_p2, .035 , .7233)
  gen z_game_p2_high = z_game_p2>.7233 & !missing(z_game_p2)
	
 * All remainning players
  
  gen z_game_p234_low = z_game_p234<-.235
  gen z_game_p234_middle = inrange(z_game_p234, -.235 , .33)
  gen z_game_p234_high = z_game_p234>.33 & !missing(z_game_p234)

  foreach x in low middle high {
	la var z_game_p1_`x' "`x' skill replaced player"
	la var z_game_p2_`x' "`x' skill entrant"
	la var z_game_p234_`x' "`x' skill team"
 }

 drop phase2score_p1 phase2score_p2 phase2score_p3 phase2score_p4 phase2score_p6 

 * Label and clean
 forv i = 1/12 {
	la var round_`i' "Round `i'"
	foreach x in ai newhire ai_direct ai_spillover newhire_direct newhire_spillover control tacit explicit {
		la var `x'Xround_`i' "`x' * Round `i'"
	}
 }	
 
 foreach i in 1 2 3 4 6 {
	la var z_game_p`i' "Skills, player `i'"
 }
 la var z_game_mean "Skills, mean"
 la var z_game_min "Skills, min"
 la var z_game_max "Skills, max"

 la var ai "AI"
 la var newhire "New hire"
 la var cfail "Coordination failures"
 la var round_diff_low "Round difficulty, low"
 la var round_diff_high "Round difficulty, high"
 
 
* 5.0 Save main dataset

sort team_id round pair

save "Data/clean data/clean_team.dta", replace



exit


